;; EX-03.scm - Adding an Effect Dynamically

(synthdef-send
 (synthdef
  "pulse"
  ((freq 1200) (rate 2))
  (let* ((trg (Decay2.ar (Impulse.ar rate 0 0.3) 0.01 0.3))
	 (osc (WhiteNoise.ar trg)))
    (Out.ar 0 (list osc osc)))))

(synthdef-send
 (synthdef
  "echo"
  ((delay 0.2) (decay 4))
  (let ((in (In.ar 0 2)))
    (ReplaceOut.ar 0 (CombN.ar in 0.5 delay decay 1 in)))))

;; Start the pulse

(-> "/s_new" "pulse" -1 1 1 "freq" 200 "rate" 1.2)

;; Add an effect.  In order to be able to send a message to the effect
;; node create and store an ID.

(begin (define N (nID))
       (-> "/s_new" "echo" N 1 1))

;; Stop the effect.

(-> "/n_free" N)

;; Add an effect (time has come today.. hey!).  Re-use N as it is
;; freed above.

(-> "/s_new" "echo" N 1 1 "delay" 0.1 "decay" 2)

;; Stop the effect

(-> "/n_free" N)

;; Stop the pulse

(-> "/g_freeAll" 1)
